package lossy;

public class DCT {
	
	private static final int N = 8;
	private double[] c = new double[N];
	
	public DCT() {
		this.initializeCoefficients();
	}
	
	private void initializeCoefficients(){
		for(int i=1; i<N;i++){
			c[i]=1;
		}
		c[0]=1.0/Math.sqrt(2.0);
	}

	public double[][] performDCT(double[][] in){
		double[][] out = new double[N][N];
		for(int a = 0; a < N; a++){
			for(int b = 0; b < N; b++){
				double sum = 0.0;
				for(int i = 0; i < N; i++){
					for(int j = 0; j < N; j++){
						sum += (in[i][j]*Math.cos((Math.PI/8.0)*(double)a*((double)i+0.5))*Math.cos((Math.PI/8.0)*(double)b*((double)j+0.5)));
					}
				}
				sum *= (c[a]*c[b]);
				out[a][b]=sum;
			}
		}
		return out;
	}
	
	public double[][] performIDCT(double[][] out){
		double[][] in = new double[N][N];
		for(int i = 0; i < N; i++){
			for(int j = 0; j < N; j++){
				double sum=0.0;
				for(int a = 0; a < N; a++){
					for(int b = 0; b < N; b++){
						sum += (c[a]*c[b]*out[a][b]*Math.cos((Math.PI/8.0)*a*(i+0.5))*Math.cos((Math.PI/8.0)*b*(j+0.5)));
					}
				}
				in[i][j] = Math.round(sum / 16.0);
			}
		}
		
		return in;
	}
	

	public static int mixColor(int red, int green, int blue) {
		return red<<16|green<<8|blue;
	}

	public static int getRed(int color) {
		return (color & 0x00ff0000)  >> 16;
	}
	
	public static int getGreen(int color) {
		return	(color & 0x0000ff00)  >> 8;
	}
	
	public static int getBlue(int color) {
		return (color & 0x000000ff)  >> 0;
		
	}
}
